package com.shg.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.shg.bean.Book;
import com.shg.bean.BuyCart;
import com.shg.bean.BuyItem;
import com.shg.bean.Users;
import com.shg.service.BookService;
import com.shg.service.BuyCartService;
import com.shg.web.tools.WebTool;

@Controller
public class BuyCartAction {

	@Resource BuyCartService buyCartService;
	@Resource BookService bookService;
	
	//购物车书籍
	private List<Book> books;
	//书籍ID
	private String bookId;
	//购物项
	private BuyItem buyItem;
	private String httpUrl;
	private String message;
	
	
	/**
	 * 购物车
	 * @return
	 */
	public String sell(){
		books=new ArrayList<Book>();
		BuyCart buyCart;
		try {
			buyCart = (BuyCart)buyCartService.select(BuyCart.class, WebTool.getLogin().getId());
			if(buyCart!=null){
				List<BuyItem> list = buyCart.initList();
				if(list.size()>0){
					books=bookService.getCatBook(list);
					//设置购买的数量
					for(Book book:books){
						for(BuyItem item:list){
							if(book.getId().equals(item.getId())){
								book.setTotal(item.getCount());
								break;
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "sell";
	}
	/**
	 * 我的购物车
	 * @return
	 */
	public String myCart(){
		sell();
		return "myCart";
	}
	/**
	 * 删除购物车里面的书籍
	 * @return
	 */
	public String deleteBook(){
		String[] ids = bookId.split(":");
		try {
			buyCartService.removeBook(WebTool.getLogin().getId(), ids);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "sell";
	}
	/**
	 * 购买书籍
	 * @return
	 */
	public String addBook(){
		BuyCart buyCart;
		try {
			buyCart = (BuyCart)buyCartService.select(BuyCart.class, WebTool.getLogin().getId());
			boolean flag=false;
			if(buyCart==null){
				buyCart=new BuyCart();
				flag=true;
			}else{
				//初始化List
				buyCart.initList();
			}
			Users user=new Users();
			user.setId(WebTool.getLogin().getId());
			//购物项
			buyCart.addItem(buyItem);
			buyCart.setUser(user);
			if(flag){
				buyCartService.insert(buyCart);
			}else{
				buyCartService.update(buyCart);
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "addBook";
	}
	/**
	 * 修改购买数量
	 * @return
	 */
	public String changeCount(){
		BuyCart buyCart;
		try {
			buyCart = (BuyCart)buyCartService.select(BuyCart.class, WebTool.getLogin().getId());
			//初始化
			buyCart.initList();
			//修改购买的数量
			buyCart.changeCount(buyItem);
			//更新
			buyCartService.update(buyCart);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
	
		return "index";
	}
	public List<Book> getBooks() {
		return books;
	}
	public void setBooks(List<Book> books) {
		this.books = books;
	}
	public String getBookId() {
		return bookId;
	}
	public void setBookId(String bookId) {
		this.bookId = bookId;
	}
	public BuyItem getBuyItem() {
		return buyItem;
	}
	public void setBuyItem(BuyItem buyItem) {
		this.buyItem = buyItem;
	}
	public String getHttpUrl() {
		return httpUrl;
	}
	public void setHttpUrl(String httpUrl) {
		this.httpUrl = httpUrl;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	
}
